package com.fc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fc.dao.CategoryMapper;
import com.fc.entity.Category;
import com.fc.entity.User;
import com.fc.entity.input.CategoryInput;
import com.fc.entity.input.CategoryQueryInput;
import com.fc.entity.input.DeleteBatchInput;
import com.fc.entity.output.CategoryOutput;
import com.fc.entity.query.PageQuery;
import com.fc.enums.CategoryEnum;
import com.fc.enums.DelFlagEnum;
import com.fc.exception.BusinessException;
import com.fc.service.CategoryService;
import com.fc.service.baseService.impl.BaseServiceImpl;
import com.fc.utils.ParseWrapper;
import com.fc.utils.ResultVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 产品分类表 服务实现类
 * </p>
 *
 * @author
 * @since 2024-11-05
 */
@Service
public class CategoryServiceImpl extends BaseServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public boolean add(CategoryInput input) {
        input.setId(null);
        check(input);
        return savePre(BeanUtil.copyProperties(input, Category.class));
    }

    private void check(CategoryInput input) {
        Category category = getOne(new LambdaQueryWrapper<Category>()
                .eq(Category::getDelFlag, DelFlagEnum.NORMAL)
                .eq(Category::getName, input.getName())
                .eq(Category::getType, input.getType())
                .ne(input.getId() != null, Category::getId, input.getId()));
        if (BeanUtil.isNotEmpty(category)) {
            throw new BusinessException(9999, "分类名称已存在");
        }
    }

    @Override
    public IPage<Category> getPage(PageQuery query, CategoryQueryInput input) {
        QueryWrapper<Category> wrapper = ParseWrapper.parseWrap(input, Category.class);
        wrapper.lambda().eq(Category::getDelFlag, DelFlagEnum.NORMAL);
        return page(Page.of(query.getPageNumber(), query.getPageSize()), wrapper);
    }

    @Override
    public boolean renew(CategoryInput input) {
        Category category = getById(input.getId());
        if (category == null || category.getDelFlag().equals(DelFlagEnum.DELETED)) {
            throw new BusinessException(9999, "分类不存在");
        }
        check(input);
        return updateByIdPre(BeanUtil.copyProperties(input, Category.class));
    }

    @Override
    public boolean del(DeleteBatchInput input) {
        List<Category> categories = input.getIds().stream().map(r -> {
            Category category = new Category();
            category.setId(r);
            category.setDelFlag(DelFlagEnum.DELETED);
            return category;
        }).toList();
        return updateBatchByIdPre(categories);
    }

    @Override
    public ResultVO<CategoryOutput> detail(Long id) {
        Category category = getById(id);
        if (BeanUtil.isEmpty(category) || category.getDelFlag().equals(DelFlagEnum.DELETED)) {
            throw new BusinessException(9999, "类型不存在");
        }
        return ResultVO.success(BeanUtil.copyProperties(category, CategoryOutput.class));
    }

    @Override
    public ResultVO<List<CategoryOutput>> getTypeList(CategoryEnum categoryEnum) {
        List<Category> categories = list(new LambdaQueryWrapper<Category>()
                .eq(Category::getDelFlag, DelFlagEnum.NORMAL)
                .eq(Category::getType, categoryEnum));
        return ResultVO.success(BeanUtil.copyToList(categories, CategoryOutput.class));
    }
}
